import { BadGatewayException, Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ModelType } from '@typegoose/typegoose/lib/types';
import { InjectModel } from 'nestjs-typegoose';
import { JobModel } from 'src/mongodb/models/jobModel/Job.models';
import { searchAppUserDto } from '../appUserApi/appUserDto/appUserDto';
import { deleteJobDto, getJobDto } from './jobDto/jobDto';

@Controller('web')
@UseGuards(AuthGuard("jwtweb"))
export class JobController {
    constructor(@InjectModel(JobModel) private jobModel: ModelType<JobModel>) { }

    /* 增 */
    @Post('addJob')
    async addJob(@Body() body: JobModel) {
        try {
            const time = Math.floor(Date.now() / 1000)
            await this.jobModel.create({ ...body, createTime: time, updateTime: time })
            return { code: 200, message: '添加成功' }
        } catch (error) {
            console.log(error);
            throw new BadGatewayException(error)
        }
    }


    /* 删 */
    @Post('deleteJob')
    async deleteJob(@Body() body: deleteJobDto) {
        try {
            await this.jobModel.deleteOne({ _id: body.id })
            return { code: 200, message: '删除成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }


    /* 删除全部 */
    @Post('deleteJobAll')
    async deleteJobAll() {
        try {
            await this.jobModel.deleteMany({})
            return { code: 200, message: '删除成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }


    /* 改 */
    @Post('updateJob')
    async updateJob(@Body() body) {
        try {
            const time = Math.floor(Date.now() / 1000)
            await this.jobModel.updateOne({ _id: body._id }, { ...body, updateTime: time })
            return { code: 200, message: '修改成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }


    /* 查 */
    @Get('getjob')
    async getJob(@Query() query: getJobDto) {
        try {
            let data: { pageNo: number | string, pageSize: number | string } = query
            if (typeof data.pageNo == 'string') {
                data.pageNo = parseInt(data.pageNo)
            }
            if (typeof data.pageSize == 'string') {
                data.pageSize = parseInt(data.pageSize)
            }
  
            const total = await this.jobModel.countDocuments()
            const userlist = await this.jobModel.find().skip((data.pageNo - 1) * data.pageSize).limit(data.pageSize).sort({ 'sort': -1 });
            return { total, data: userlist }

        } catch (error) {
            throw new BadGatewayException(error)
        }
    }

    /* 搜索 */
    @Get('searchjob')
    async searchjob(@Query() query: searchAppUserDto) {
        try {
            const reg = new RegExp(query.name)
            const userlist = await this.jobModel.find({ title: { $regex: reg } })
            return { total: userlist.length, data: userlist }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }
}
